	
	
function $(selector){
	return document.querySelectorAll(selector);
}
/**判断是否存在某个class**/
function hasClass(element, cls) {
	return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}


var slide = $(".slide"),
	ui_pager = $(".ui-pager-item"),
	btnPrev = $(".ui-prev")[0],
	btnNext = $(".ui-next")[0],
	cateChildren = $(".children"),
	btns = $(".home-stars-goods .more .control"),
    goods_list = $(".goods-list")[0];


/**主页轮播图**/
function  uiViewport(){
	var j,m,d;
	for(var i = 0;i<slide.length;i++){
		hasClass(slide[i],"slide-active")==true?j=i:"";
		
		(function(i){
			ui_pager[i].onclick = function(){
				d = i;
				ani(j,d);
				j = d;
			}
			//mouseover清除定时器
			slide[i].onmouseover = function(){
				clearInterval(t);
			}
			slide[i].onmouseout = function(){
				t = setInterval(function(){
					d = j;
					j>=slide.length-1?j=0:j +=1;
					ani(d,j);
				},6000);
			}
		})(i)
	}
	
	//添加定时器
	var t = setInterval(function(){
		d = j;
		j>=slide.length-1?j=0:j +=1;
		ani(d,j);
	},6000);
	
	btnPrev.addEventListener("click",function(){
		d = j;
		j<=0?j=slide.length-1:j-=1;
		ani(d,j);
		clearInterval(t);
	});
	btnNext.addEventListener("click",function(){
		d = j;
		j>=slide.length-1?j=0:j +=1;
		ani(d,j);
		clearInterval(t);
	});
	
	function ani(d,j){
		slide[d].classList.remove("slide-active");
		slide[j].classList.add("slide-active");
		ui_pager[d].classList.remove("pager-active");
		ui_pager[j].classList.add("pager-active");
	}
}
uiViewport();


//明星单品轮播图
var speed = goods_list.offsetWidth/2;
function shuffing(){
	var mLeft = parseInt(goods_list.style.left);
	
	(isNaN(mLeft)||mLeft==0)?run(1,0):run(0,1);

	btns[0].addEventListener("click",function(){
		run(0,1);
	});
	btns[1].addEventListener("click",function(){
		run(1,0);
	});
}
function run(index,i){
	btns[i].classList.remove("control-disabled");
	btns[index].classList.add("control-disabled");
	goods_list.style.left = -index*speed +"px";
	
}
setInterval(shuffing,8000);


//内容部分轮播图
function contentItem(){
	
	var book = $(".book .item-list")[0],
	    num = book.childElementCount;
	
	var speed = book.offsetWidth/num;
	
	content(".book");
	content(".theme");
	content(".game");
	content(".app");
	
	function content(selector){
		
		var btns = $(selector+"  .control"),
		    liLength  = $(selector+"  .page-wrap i");
		    
		var index = 0;
		for(var i = 0;i<liLength.length;i++){
			hasClass(liLength[i],"slide-active")==true?index=i:"";
			/**pager点击跳转**/
			(function(i){
				$(selector+"  .page-wrap i")[i].addEventListener("click",function(){
					page(selector,i);
				})
			})(i)
		}
		    
		btns[0].addEventListener("click",function(){
			index--;
			index<0?index = 0:"";
			page(selector,index);
		});
		btns[1].addEventListener("click",function(){
			index++;
			index>liLength.length-1?index = liLength.length-1:"";
			page(selector,index);
		});
		function page(selector,index){
			$(selector+"  .item-list")[0].style.left = index*-1*speed+"px";
			$(selector+"  .page-active")[0].classList.remove("page-active");
			liLength[index].classList.add("page-active");
		}
	}
}
contentItem();


/**为你推荐轮播图**/
function recommend(){
	var moreBtn = $(".home-recm-box .more a"),
		recommend_list = $(".home-recm-box ul")[0];
		
	var width = parseInt(recommend_list.offsetWidth),
		mLeft,
	    index = 0;
	
	moreBtn[0].addEventListener("click",function(){
		mLeft = parseInt(recommend_list.style.left);
		if(isNaN(mLeft)||mLeft==0){
			moreBtn[0].classList.add("control-disabled");
		}else{
			moreBtn[0].classList.remove("control-disabled");
			moreBtn[1].classList.remove("control-disabled");
			index--;
		}
		index<=0?moreBtn[0].classList.add("control-disabled"):"";
		recommend_list.style.left = -1*index*speed+"px";
	});
	moreBtn[1].addEventListener("click",function(){
		mLeft = Math.abs(parseInt(recommend_list.style.left));
		if(mLeft>=(width-speed)){
			moreBtn[1].classList.add("control-disabled");
		}else{
			moreBtn[1].classList.remove("control-disabled");
			moreBtn[0].classList.remove("control-disabled");
			index++;
		}
		index>=3?moreBtn[1].classList.add("control-disabled"):"";
		recommend_list.style.left = -1*index*speed+"px";
	});	
}
recommend();
	
	
	
	
